// token 验证

const jwt = require("jsonwebtoken")
const config = require("../config")
const User = require("../model/user/user")

const need_token = async (token) => {
    try {
        token = token.slice(0,-21)
        let arr = token.split("@")
        let username = jwt.verify(arr[0], config.jwt.secret)
        let res = await User.findOne({
            attributes: ["password"],
            where: { username }
        })
        if (res) {
            let veriFyPassword = jwt.verify(res.password, config.jwt.secret)
            let headersPassword = jwt.verify(arr[1], config.jwt.secret)
            if (headersPassword === veriFyPassword) return { msg: "token验证通过", code: "200", username }
            return { msg: "token验证未通过", code: "400" }
        } else {
            return { msg: "token验证未通过", code: "400" }
        }
    } catch (err) { return { msg: "token验证未通过", code: "400", err } }
}
module.exports = {
    need_token
}